package com.yic.module.base.service.factory;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.ObjectUtil;
import com.yic.framework.common.util.collection.CollectionUtils;
import com.yic.module.base.dal.redis.RedisKeyConstants;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;

import java.util.*;
import com.yic.module.base.controller.admin.factory.vo.*;
import com.yic.module.base.dal.dataobject.factory.FactoryDO;
import com.yic.framework.common.pojo.PageResult;

import com.yic.module.base.convert.factory.FactoryConvert;
import com.yic.module.base.dal.mysql.factory.FactoryMapper;

import static com.yic.framework.common.exception.util.ServiceExceptionUtil.exception;
import static com.yic.module.base.enums.ErrorCodeConstants.*;

/**
 * 工厂 Service 实现类
 *
 * @author 系统管理员
 */
@Service
@Validated
public class FactoryServiceImpl implements FactoryService {

    @Resource
    private FactoryMapper factoryMapper;

    @Override
    public Long createFactory(FactoryCreateReqVO createReqVO) {
        // 校验工厂名称唯一性
        validateFactoryNameExists(createReqVO.getName());
        // 校验工厂代码唯一性
        validateFactoryCodeExists(createReqVO.getCode());
        // 插入
        FactoryDO factory = FactoryConvert.INSTANCE.convert(createReqVO);
        factoryMapper.insert(factory);
        // 返回
        return factory.getId();
    }

    @Override
    @CacheEvict(cacheNames = RedisKeyConstants.FACTORY, key = "#updateReqVO.id")
    public void updateFactory(FactoryUpdateReqVO updateReqVO) {
        // 校验存在
        FactoryDO factory = getFactory(updateReqVO.getId());
        if (ObjectUtil.isEmpty(factory)){
            throw exception(FACTORY_NOT_EXISTS);
        }
        if (!updateReqVO.getName().equals(factory.getName())){
            // 名称已被修改，需要校验名称是否重复
            validateFactoryNameExists(updateReqVO.getName());
        }
        if (!updateReqVO.getCode().equals(factory.getCode())){
            // 代码已被修改，需要检验代码是否重复
            validateFactoryCodeExists(updateReqVO.getCode());
        }
        // 更新
        FactoryDO updateObj = FactoryConvert.INSTANCE.convert(updateReqVO);
        factoryMapper.updateById(updateObj);
    }

    @Override
    @CacheEvict(cacheNames = RedisKeyConstants.FACTORY, key = "#id")
    public void deleteFactory(Long id) {
        // 校验存在
        validateFactoryExists(id);
        // 删除
        factoryMapper.deleteById(id);
    }

    private void validateFactoryExists(Long id) {
        if (factoryMapper.selectById(id) == null) {
            throw exception(FACTORY_NOT_EXISTS);
        }
    }

    private void validateFactoryNameExists(String name){
        if (ObjectUtil.isNotEmpty(factoryMapper.selectByName(name))){
            throw exception(FACTORY_NAME_EXISTS);
        }
    }

    private void validateFactoryCodeExists(String code) {
        if (ObjectUtil.isNotEmpty(factoryMapper.selectByCode(code))){
            throw exception(FACTORY_CODE_EXISTS);
        }
    }

    @Override
    @Cacheable(cacheNames = RedisKeyConstants.FACTORY, key = "#id")
    public FactoryDO getFactory(Long id) {
        return factoryMapper.selectById(id);
    }

    @Override
    public List<FactoryDO> getFactoryList(Collection<Long> ids) {
        return factoryMapper.selectBatchIds(ids);
    }

    @Override
    public PageResult<FactoryDO> getFactoryPage(FactoryPageReqVO pageReqVO) {
        return factoryMapper.selectPage(pageReqVO);
    }

    @Override
    public List<FactoryDO> getFactoryList(FactoryExportReqVO exportReqVO) {
        return factoryMapper.selectList(exportReqVO);
    }

    @Override
    public List<FactoryDO> getFactoryList(Collection<Long> ids, Collection<Integer> statuses) {
        return factoryMapper.selectList(ids, statuses);
    }

    @Override
    public Map<Long, FactoryDO> getFactoryMap(List<Long> ids) {
        if (CollUtil.isEmpty(ids)){
            return Collections.emptyMap();
        }
        List<FactoryDO> list = factoryMapper.selectBatchIds(ids);
        return CollectionUtils.convertMap(list, FactoryDO::getId);
    }

    @Override
    public FactoryDO getFactory(String code) {
        return factoryMapper.selectByCode(code);
    }

    @Override
    public List<FactoryDO> getFactoryList() {
        return factoryMapper.selectList();
    }

}
